package at.fhj.swd10.eshop.converter;

import at.fhj.swd10.eshop.domain.Category;
import at.fhj.swd10.eshop.web.application.ShopData;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.commons.lang.StringUtils;

/**
 * @author David
 */
@FacesConverter("at.fhj.swd10.eshop.converter.categoryConverter")
public class CategoryConverter implements Converter {

    private ShopData shopData;

    public CategoryConverter() throws Exception {
        shopData = new ShopData();
    }

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        List<Category> categoryList = shopData.getCategoryList();
        for (Category category : categoryList) {
            if (StringUtils.equals(category.getName(), string)) {
                return category;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        Category category = (Category) o;
        return category.getName();
    }
}
